1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 import java.net.*;
30 import java.util.Enumeration;
31
32 public class Test {
33
34 static Object[][] getTestCombinations() {
35 return new Object[][] {
36 { "ServerSocket", "Socket" },
37 { "Socket", "Socket" },
38 { "DatagramSocket", "DatagramSocket" },
39 };
40 }
41
42 static InetAddress ia4_this;
43 static InetAddress ia6_this;
44
45 static int count;
46 static int failures;
47
48 static void doTest(Object test[], InetAddress ia1, InetAddress ia2,
49 boolean silent) {
50 String s1_type = (String)test[0];
51 String s2_type = (String)test[1];
52 int port = 0;
53
54
55
56
57 count++;
58
59
60
61
62
63 boolean gotBindException = false;
64 boolean failed = false;
65 Exception failed_exc = null;
66
67 try {
68 Socket sock1, sock2;
69 ServerSocket ss;
70 DatagramSocket dsock1, dsock2;
71
72
73
74 if (s1_type.equals("Socket")) {
75 sock1 = new Socket();
76 sock1.bind( new InetSocketAddress(ia1, 0));
77 port = sock1.getLocalPort();
78 }
79
80 if (s1_type.equals("ServerSocket")) {
81 ss = new ServerSocket(0, 0, ia1);
82 port = ss.getLocalPort();
83 }
84
85 if (s1_type.equals("DatagramSocket")) {
86 dsock1 = new DatagramSocket( new InetSocketAddress(ia1, 0) );
87 port = dsock1.getLocalPort();
88 }
89
90
91
92 if (s2_type.equals("Socket")) {
93 sock2 = new Socket();
94 sock2.bind( new InetSocketAddress(ia2, port));
95 }
96
97 if (s2_type.equals("ServerSocket")) {
98 ss = new ServerSocket(port, 0, ia2);
99 }
100
101 if (s2_type.equals("DatagramSocket")) {
102 dsock2 = new DatagramSocket( new InetSocketAddress(ia2, port) );
103 }
104
105 } catch (BindException be) {
106 gotBindException = true;
107 } catch (Exception e) {
108 failed = true;
109 failed_exc = e;
110 }
111
112
113
114
115 boolean expectedBindException = true;
116 if (ia1 == ia4_this && ia2 == ia6_this) {
117 expectedBindException = false;
118 }
119 if (ia1 == ia6_this && ia2 == ia4_this) {
120 expectedBindException = false;
121 }
122
123
124
125
126
127 if (!failed && gotBindException != expectedBindException) {
128 failed = true;
129 }
130
131
132
133
134 if (!failed && silent) {
135 return;
136 }
137
138 if (failed || !silent) {
139 System.out.println("");
140 System.out.println("**************************");
141 System.out.println("Test " + count);
142
143 System.out.println(s1_type + " binds: " + ia1 + " port: " + port);
144 System.out.println(s2_type + " binds: " + ia2);
145
146 if (!failed) {
147 if (gotBindException) {
148 System.out.println("Got expected BindException - test passed!");
149 } else {
150 System.out.println("No BindException as expected - test passed!");
151 }
152 return;
153 }
154 }
155 if (gotBindException) {
156 System.out.println("BindException unexpected - test failed!!!");
157 } else {
158 System.out.println("No bind failure as expected - test failed!!!");
159 }
160 failures++;
161 }
162
163 public static void main(String args[]) throws Exception {
164
165 boolean silent = true;
166 if (args.length > 0) {
167 if (args[0].equals("-d")) {
168 silent = false;
169 }
170 }
171
172
173
174
175 Enumeration nifs = NetworkInterface.getNetworkInterfaces();
176 while (nifs.hasMoreElements()) {
177 NetworkInterface ni = (NetworkInterface)nifs.nextElement();
178
179 Enumeration addrs = ni.getInetAddresses();
180 while (addrs.hasMoreElements()) {
181 InetAddress ia = (InetAddress)addrs.nextElement();
182
183 if (ia.isLoopbackAddress() || ia.isAnyLocalAddress()) {
184 continue;
185 }
186
187 if ((ia instanceof Inet4Address) && (ia4_this == null)) {
188 ia4_this = ia;
189 }
190
191 if ((ia instanceof Inet6Address) && (ia6_this == null)) {
192 ia6_this = ia;
193 }
194 }
195 }
196
197
198
199
200
201 InetAddress addrs[] = { ia4_this, ia6_this };
202
203 Object tests[][] = getTestCombinations();
204
205 for (int i=0; i<tests.length; i++) {
206 Object test[] = tests[i];
207
208 for (int j=0; j<addrs.length; j++) {
209 for (int k=0; k<addrs.length; k++) {
210
211 if (addrs[j] == null || addrs[k] == null) {
212 continue;
213 }
214
215 doTest( test, addrs[j], addrs[k], silent);
216 }
217 }
218 }
219
220 System.out.println("");
221 System.out.println(count + " test(s) executed. " + failures + " failure(s).");
222
223 if (failures > 0) {
224 throw new Exception(failures + " tests(s) failed - see log");
225 }
226 }
227 }